package me.keiwu.admin.controller;

import me.keiwu.admin.domain.WXQRCode;
import me.keiwu.admin.service.WXQRCodeService;
import me.keiwu.pub.data.RGB;
import me.keiwu.pub.data.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by kei on 06/12/2017.
 */
@RestController
@RequestMapping("/api/weixin")
public class WeixinController {

    @Autowired
    private WXQRCodeService wxqrCodeService;


    @RequestMapping("/qrcode/list")
    public Response createQRCode(@RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "10") Integer size) {
        return Response.ok(wxqrCodeService.listQRCode(page, size));
    }


    @RequestMapping("/qrcode/create")
    public Response createQRCode(@RequestParam(required = false) Integer id,
                                 String sceneStr,
                                 String description,
                                 String page,
                                 @RequestParam(required = false) Integer width,
                                 @RequestParam(required = false) Integer r,
                                 @RequestParam(required = false) Integer g,
                                 @RequestParam(required = false) Integer b) {
        RGB lineColor = new RGB();
        if (r != null && g != null && b != null) {
            lineColor.setR(r);
            lineColor.setG(g);
            lineColor.setB(b);
        }
        WXQRCode code = wxqrCodeService.createQRCode(id, sceneStr, description, page, width, lineColor);
        if (code == null) return Response.fail("已存在该参数的二维码");
        return Response.ok(code);
    }

}
